<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Core
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-06-06
 ***********************************************************************/
if(defined("IVANENGINE_CORE_STRINGHELPER"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_STRINGHELPER", true);

require_once (dirname(__FILE__).'/../Common.php');

class CStringHelper extends CBase
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function __destruct()
    {
        parent::__destruct();
    }
    
    static public function ConvertTag($string, $t1 = '&lt;', $t2 = '&gt;')
    {
        $string   = str_replace('>', $t1, $string);
        $string   = str_replace('<', $t2, $string);
        return $string;
    }
    
    static public function ConvertWarp($string, $desc = '<br>')
    {
        $string = str_replace(chr(10), $desc, $string);
        $string = str_replace(chr(13), '', $string);
        return $string;
    }
    
    static public function ConvertQuot($string, $quot = '&quot;', $sQuot = '&#39;')
    {
        $string   = str_replace('"', $quot, $string);
        $string   = str_replace("'", $sQuot, $string);
        return $string;
    }
    
    static public function ToJS($string)
    {
        $string = self::ConvertWarp($string, '');
        $string   = str_replace('"', '&quot;', $string);
        $string = addslashes($string);
        $string   = str_replace('&quot;', '"', $string);
        return $string;
    }
    
    static public function JSCodeInHtmlEvent($string)
    {
        // stripslashes
        return addslashes($string);
    }
    
    static public function ToText($string)
    {
        $string = self::ConvertWarp($string);
        $string = self::ConvertTag($string);
        $string = self::ConvertQuot($string);
        return $string;
    }
    
    static public function ToSQL($string)
    {
        $string = self::ConvertWarp($string);
        $string = self::ConvertQuot($string);
        return $string;
    }
    
    static public function ToUpper($string)
    {
        return strtoupper($string);
    }
    
    static public function ToLower($string)
    {
        return strtolower($string);
    }
    
    static public function FindLastOf($findStr, $string, $beginPos = CString::npos)
    {
        $length = strlen($string);
        if ($length == 0 || $findStr == "" || $findStr[0] == '\0'){
            return self::npos;
        }

        if ($beginPos >= $length || $beginPos == CString::npos){
            $beginPos = $length - 1;
        }

        while($beginPos > 0)
        {
            --$beginPos;
            if (strchr($string, $string[$beginPos]))
                return $beginPos;
        }

        return self::npos;
    }
    
    static public function FindFirstOf($findStr, $string, $beginPos = 0)
    {
        $length = strlen($string);
        if ($length == 0 || $findStr == "" || $findStr[0] == '\0'){
            return self::npos;
        }

        $pos = strpos(substr($string, $beginPos, $length-$beginPos), $findStr);

        if( $pos != false ){
            return $pos;
        }
        return CString::npos;
    }
    
    static public function ToInt($string)
    {
        if( $string && is_string($string) ){
            return intval($string);
        }
        return -1;
    }
    
    static public function ToBool($string)
    {
        if( $string === true){
            return true;
        }
        
        if( !$string ){
            return false;
        }
        
        if( is_object($string) ){
            return true;
        }
        $string = self::ToLower($string);
        if( $string == 'true' || $string == 'ok' || $string == 'yes' ||
            ( is_integer($string) && intval($string) > 0 ) 
        )
        {
            return true;
        }
        return false;
    }
    
    static public function ToFloat($string)
    {
        if( $string && is_string($string) ){
            return floatval($string);
        }
        return 0.0;
    }
    
    static public function ToArray($string)
    {
        preg_match_all('/
          \s*(\w+)              # key                               \\1
          \s*=\s*               # =
          (\'|")?               # values may be included in \' or " \\2
          (.*?)                 # value                             \\3
          (?(2) \\2)            # matching \' or " if needed        \\4
          \s*(?:
            (?=\w+\s*=) | \s*$  # followed by another key= or the end of the string
          )
        /x', $string, $matches, PREG_SET_ORDER);

        $attributes = array();
        foreach ($matches as $val)
        {
            $attributes[$val[1]] = self::Literalize($val[3]);
        }

        return $attributes;
    }
    
    static public function Literalize($string)
    {
        // lowercase our value for comparison
        $value  = trim($value);
        $lvalue = strtolower($value);

        if (in_array($lvalue, array('null', '~', '')))
        {
            $value = null;
        }
        else if (in_array($lvalue, array('true', 'on', '+', 'yes')))
        {
            $value = true;
        }
        else if (in_array($lvalue, array('false', 'off', '-', 'no')))
        {
            $value = false;
        }
        else if (ctype_digit($value))
        {
            $value = (int) $value;
        }
        else if (is_numeric($value))
        {
            $value = (float) $value;
        }
        else
        {
            $value = is_string($value) ? preg_replace_callback('/%(.+?)%/', 
                create_function('$v', 'return "%{$v[1]}%";'), $value) : $value;
            if ($quoted)
            {
                $value = '\''.str_replace('\'', '\\\'', $value).'\'';
            }
        }
        
        return $value;
    }
    
    static public function ExtractScriptCode($content)
    {
        $result     = array($content, array());
        
        if( empty($content) ){
            return array(null, array());
        }
        
        $array      = array();
        $getCodeReg = '#<script[^>]*>(?>[^<]*)(?>(?!</?script>)<[^<]*)*</script>#is';
        $filterReg  = "/<(\/?script.*?)>/si";
        preg_match_all($getCodeReg, $content, $array);
        foreach( $array as $scripts ){
            if( is_array($scripts) ){
                foreach( $scripts as $script ){
                    $script = preg_replace($filterReg, "", $script);
                    $result[1][] = stripslashes($script);
                    //$this->AddScript(stripslashes($script), $previously);
                }
            }else{
                $scripts = preg_replace($filterReg,"", $scripts);
                $result[1][] = stripslashes($script);
                //$this->AddScript($scripts, $previously);
            }
        }
        $result[0] = preg_replace($getCodeReg, '', $content);
        
        return $result;
    }
}

class HString extends CStringHelper{}
class HStr extends HString{}
?>
